package com.tpc.yaoxpt.utils;

import org.ini4j.Profile;
import org.ini4j.Wini;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class FileUtil {

    public static String filePath = "";

    public static String getFilePath() throws IOException {
        File file =new File("");
        String filePath = null;
        filePath = file.getCanonicalPath() + "/Config.ini";
        return filePath;
    }

    public static Map<String,String> getTerraceConfig() throws IOException {
        Map<String,String> map = new HashMap<>();
        filePath = FileUtil.getFilePath();
        Wini wini = new Wini(new File(filePath));
        Profile.Section section = wini.get("TerraceConfig");
        String URL = section.get("URL");
        String OrgUserName = section.get("OrgUserName");
        String Secret = section.get("Secret");
        String accessToken = section.get("AccessToken");
        String expiresDate = section.get("ExpiresDate");
        map.put("URL",URL);
        map.put("OrgUserName",OrgUserName);
        map.put("Secret",Secret);
        map.put("AccessToken",accessToken);
        map.put("ExpiresDate",expiresDate);
        return map;
    }

    public static Map<String,String> getDBConfig() throws IOException {
            Map<String,String> map = new HashMap<>();
            filePath = FileUtil.getFilePath();
            Wini wini = new Wini(new File(filePath));
            Profile.Section section = wini.get("DBCONFIG");
            map.put("DBTYPE",section.get("DBTYPE"));
            map.put("DBIP",section.get("DBIP"));
            map.put("DBUSER",section.get("DBUSER"));
            map.put("DBPASSWORD",section.get("DBPASSWORD"));
            map.put("DBNAME",section.get("DBNAME"));
            map.put("DBPORT",section.get("DBPORT"));
            return map;
    }

    public static Map<String,String> getPTConfig() throws IOException {
        Map<String,String> map = new HashMap<>();
        filePath = FileUtil.getFilePath();
        Wini wini = new Wini(new File(filePath));
        Profile.Section section = wini.get("PTCONFIG");
        map.put("ENTNAME",section.get("ENTNAME"));
        map.put("URL",section.get("URL"));
        map.put("APIKEY",section.get("APIKEY"));
        map.put("SECRETKEY",section.get("SECRETKEY"));

        return map;
    }

    public static Map<String,String> getConfigConfig() throws IOException {
        Map<String,String> map = new HashMap<>();
        filePath = FileUtil.getFilePath();
        Wini wini = new Wini(new File(filePath));
        Profile.Section section = wini.get("CONFIG");
        map.put("LOGCOUNT",section.get("LOGCOUNT"));
        return map;
    }

    public static Map<String,String> getInterface(String business) throws IOException {
        Map<String,String> map = new HashMap<>();
        filePath = FileUtil.getFilePath();
        Wini wini = new Wini(new File(filePath));
        Profile.Section section = wini.get("接口定义");
        String businessValue = section.get(business);
        map.put(business,businessValue);
        return map;
    }

    public static Map<String,String> getTableName(String business) throws IOException {
        Map<String,String> map = new HashMap<>();
        filePath = FileUtil.getFilePath();
        Wini wini = new Wini(new File(filePath));
        Profile.Section section = wini.get("表名");
        String businessValue = section.get(business);
        map.put(business,businessValue);
        return map;
    }

    public static Map<String,String> getQuerySql(String business) throws IOException {
        Map<String,String> map = new HashMap<>();
        filePath = FileUtil.getFilePath();
        Wini wini = new Wini(new File(filePath));
        Profile.Section section = wini.get("SQL查询");
        String businessValue = section.get(business);
        map.put(business,businessValue);
        return map;
    }

    public static Map<String,String> getBackSql(String business) throws IOException {
        Map<String,String> map = new HashMap<>();
        filePath = FileUtil.getFilePath();
        Wini wini = new Wini(new File(filePath));
        Profile.Section section = wini.get("SQL回写");
        String businessValue = section.get(business);
        map.put(business,businessValue);
        return map;
    }

    public static Map<String,String> getLastTime(String business) throws IOException {
        Map<String,String> map = new HashMap<>();
        filePath = FileUtil.getFilePath();
        Wini wini = new Wini(new File(filePath));
        Profile.Section section = wini.get("任务时间");
        String businessValue = section.get(business);
        map.put(business,businessValue);
        return map;
    }

    public static Map<String,String> getMaxTime(String business) throws IOException {
        Map<String,String> map = new HashMap<>();
        filePath = FileUtil.getFilePath();
        Wini wini = new Wini(new File(filePath));
        Profile.Section section = wini.get("最大时间");
        String businessValue = section.get(business);
        if(businessValue==null){
            businessValue = "2020-01-01 00:00:00";
        }
        map.put(business,businessValue);
        return map;
    }

    public static Map<String,String> getCheckData(String business) throws IOException {
        Map<String,String> map = new HashMap<>();
        filePath = FileUtil.getFilePath();
        Wini wini = new Wini(new File(filePath));
        Profile.Section section = wini.get("校验数据");
        String businessValue = section.get(business);
        map.put(business,businessValue);
        return map;
    }

    public static Map<String,String> getCheckField(String business) throws IOException {
        Map<String,String> map = new HashMap<>();
        filePath = FileUtil.getFilePath();
        Wini wini = new Wini(new File(filePath));
        Profile.Section section = wini.get("校验字段");
        String businessValue = section.get(business);
        map.put(business,businessValue);
        return map;
    }

    public static String getValue(String sectionName,String key)  {
        Map<String,String> map = new HashMap<>();
        try {
            filePath = FileUtil.getFilePath();
            Wini wini = new Wini(new File(filePath));
            Profile.Section section = wini.get(sectionName);
            return section.get(key);
        } catch (IOException e) {
            AlertUtil.FAIL(e.getMessage());
            throw new RuntimeException(e);
        }

    }

    public static void addSection(String section,String key,String value) throws IOException {

            filePath = FileUtil.getFilePath();
            Wini wini = new Wini(new File(filePath));
            wini.add(section,key,value);
            wini.store(new File(filePath));


    }
}
